package com.seantone.inappupdate.plugin.google;

import android.app.Activity;
import android.content.Context;
import android.content.IntentSender;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.IntentSenderRequest;
import androidx.activity.result.contract.ActivityResultContracts;

import com.google.android.gms.tasks.Task;
import com.google.android.play.core.appupdate.AppUpdateInfo;
import com.google.android.play.core.appupdate.AppUpdateManager;
import com.google.android.play.core.appupdate.AppUpdateManagerFactory;
import com.google.android.play.core.appupdate.AppUpdateOptions;
import com.google.android.play.core.install.model.AppUpdateType;
import com.google.android.play.core.install.model.UpdateAvailability;
import com.seantone.inappupdate.core.define.CheckUpdateParams;
import com.seantone.inappupdate.core.define.CheckUpdateRet;
import com.seantone.inappupdate.core.impl.ICheckUpdateCallBack;
import com.seantone.inappupdate.core.impl.IInAppUpdate;
import com.seantone.inappupdate.core.impl.IRequestUpdate;
import com.seantone.inappupdate.core.utils.Utils;

public class SDK implements IInAppUpdate {
    AppUpdateManager appUpdateManager = null;
    AppUpdateInfo appUpdateInfo = null;

    @Override
    public boolean isBest(Context context) {
        // 如谷歌的应用商城，那就是最合适的
        return Utils.verifyInstallerId(context, "com.android.vending");
    }

    @Override
    public boolean isSupport(Context context) {
        if (isBest(context)) return true;
        return false;
    }

    @Override
    public void checkUpdate(Context context, CheckUpdateParams params, ICheckUpdateCallBack callBack) {
        this.appUpdateManager = AppUpdateManagerFactory.create(context);
        Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
               this.appUpdateInfo = appUpdateInfo;
               if( params.auto){
                   this.requestUpdate(context, null);
               }else{
                   CheckUpdateRet ret = new CheckUpdateRet();
                   ret.versionCode = appUpdateInfo.availableVersionCode();
                   callBack.onRet(ret);
               }
            }
        });
    }

    @Override
    public void requestUpdate(Context context, IRequestUpdate callBack) {
        if (appUpdateManager == null) {
            return;
        }

        try {
            appUpdateManager.startUpdateFlowForResult(
                    // Pass the intent that is returned by 'getAppUpdateInfo()'.
                    appUpdateInfo,
                    // an activity result launcher registered via registerForActivityResult
                    // activityResultLauncher,
                    (Activity) context,
                    // Or pass 'AppUpdateType.FLEXIBLE' to newBuilder() for
                    // flexible updates.
                    AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build(), 0);
        } catch (IntentSender.SendIntentException e) {
            throw new RuntimeException(e);
        }
    }
}
